import * as THREE from 'three';
// 引入gltf模型加载库GLTFLoader.js
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { FirstPersonControls } from 'three/examples/jsm/controls/FirstPersonControls.js';
// 三维场景
const scene = new THREE.Scene();
// 创建加载器对象
const loader = new GLTFLoader();
loader.load('./buildings.glb', function (gltf) {
    gltf.scene.scale.set(50, 50, 50);
    scene.add(gltf.scene);
})
//设置平行光，使建筑除底部外剩余五面均被光照
const directionalLight1 = new THREE.DirectionalLight(0xffffff, 1);
directionalLight1.position.set(0, 1000, 0);
scene.add(directionalLight1);
const directionalLight2 = new THREE.DirectionalLight(0xffffff, 1);
directionalLight2.position.set(0, 0, 1000);
scene.add(directionalLight2);
const directionalLight3 = new THREE.DirectionalLight(0xffffff, 1);
directionalLight3.position.set(1000, 0, 0);
scene.add(directionalLight3);
const directionalLight4 = new THREE.DirectionalLight(0xffffff, 1);
directionalLight4.position.set(-1000, 0, 0);
scene.add(directionalLight4);
const directionalLight5 = new THREE.DirectionalLight(0xffffff, 1);
directionalLight5.position.set(0, 0, -1000);
scene.add(directionalLight5);
// AxesHelper：辅助观察的坐标系
const axesHelper = new THREE.AxesHelper(500);
scene.add(axesHelper);
const width = window.innerWidth;
const height = window.innerHeight;
// 相机
const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
camera.position.set(200, 100, 200);
camera.lookAt(0, 120, 0);
// WebGL渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
//three.js执行渲染命令会输出一个canvas画布(HTML元素)，你可以插入到web页面中
document.body.appendChild(renderer.domElement);
// 设置第一人称控件
const controls = new FirstPersonControls(camera, renderer.domElement);
controls.enabled = true;
controls.lookSpeed = 0.04; //鼠标移动查看的速度
controls.movementSpeed = 40; //相机移动速度
controls.noFly = false;
controls.constrainVertical = true; //约束垂直
controls.verticalMin = 0;
controls.verticalMax = 2.5;
controls.lon = 0; //进入初始视角x轴的角度
controls.lat = 0; //初始视角进入后y轴的角度
const clock = new THREE.Clock();
const render = function () {
    const delta = clock.getDelta(); //获取自上次调用的时间差
    controls.object.position.y = 120;
    controls.update(delta); //更新第一人称控件
    renderer.render(scene, camera);
    requestAnimationFrame(render);
}
render();



